package top.huanyv.tools.utils;

import java.util.regex.Pattern;

/**
 * @author huanyv
 * @date 2023/10/17 13:58
 */
public final class RegexUtil {
    private RegexUtil() {
    }

    public static final String NUMBER_REGEX = "^[0-9]*$";
    public static final String CHINESE_REGEX = "^[\\u4E00-\\u9FA5]+$";
    public static final String PHONE_NUMBER_REGEX = "^(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[189]))\\d{8}$";
    public static final String EMAIL_REGEX = "^(?:0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\\d{4}$";
    public static final String QQ_NUMBER_REGEX = "^[1-9][0-9]{4,}$";
    public static final String DOMAIN_NAME_REGEX = "^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(?:\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$";
    public static final String WEB_LINK_REGEX = "^(?:https?:\\/\\/)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(?:\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$";
    public static final String IPV4_REGEX = "^(?:(?:\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(?:\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])$";
    public static final String IPV6_REGEX = "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$";

    /**
     * 字符串与正则是否全部匹配
     * @param s 字符串
     * @param regex 正则表达式
     * @return boolean 是、否
     */
    public boolean matches(String s, String regex) {
        return Pattern.compile(regex).matcher(s).matches();
    }

    public boolean find(String s, String regex) {
        return Pattern.compile(regex).matcher(s).find();
    }

    public boolean isNumber(String s) {
        return matches(s, NUMBER_REGEX);
    }

    public boolean isChinese(String s) {
        return matches(s, CHINESE_REGEX);
    }

    public boolean isPhoneNumber(String s) {
        return matches(s, PHONE_NUMBER_REGEX);
    }

    public boolean isEmail(String s) {
        return matches(s, EMAIL_REGEX);
    }

    public boolean isQQNumber(String s) {
        return matches(s, QQ_NUMBER_REGEX);
    }
}
